package com.prs.crm.action.oa;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.oa.CarPermission;
import com.prs.crm.service.hr.DeptService;
import com.prs.crm.service.oa.CarPermissionService;

public class CarPermissionAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private CarPermission carPermission;
	private CarPermissionService carPermissionService;
	
	private PaginationSupport pagination;
	
	private Integer[]  ids;
	private List<Dept> dept;
	private DeptService deptService;
	public CarPermission getCarPermission() {
		return carPermission;
	}
	public void setCarPermission(CarPermission carPermission) {
		this.carPermission = carPermission;
	}
	public CarPermissionService getCarPermissionService() {
		return carPermissionService;
	}
	public void setCarPermissionService(CarPermissionService carPermissionService) {
		this.carPermissionService = carPermissionService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public List<Dept> getDept() {
		return dept;
	}
	public void setDept(List<Dept> dept) {
		this.dept = dept;
	}
	public DeptService getDeptService() {
		return deptService;
	}
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	
	@Action("listCarPermission")
	public String listCarPermission(){
		DetachedCriteria criteria =this.getPaginationHelper().createQuery(CarPermission.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCarPermissionService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editCarPermission")
	public String editCarPermission(){
		this.dept=this.getDeptService().getAllDept();
		return SUCCESS;
	}
	
	@Clew(results = { "success", "none" })
	@Action(value = "saveCarPermission", results = {
			@Result(name = "success", type = "redirect", location = "listCarPermission"),
			@Result(name = "error", type = "chain", location = "editCarPermission") })
	public String saveCarPermission(){
		if (carPermission.getId()==null) {
			List<CarPermission> ca=this.getCarPermissionService().getAllCarPermission();
			int c=0;
			for (CarPermission car : ca) {
				if (carPermission.getDept().equals(car.getDept())) {
					c++;
				}
			}
			if (c>0) {
				this.addActionError("车辆所属不能重复！");
				return ERROR;
			}
			this.getCarPermissionService().save(carPermission);
		}else{
			this.getCarPermissionService().submit(carPermission);
		}
		this.setPrompt("添加成功！");
		return SUCCESS;
	}
	@Action(value = "removeCarPermission", results = {
			@Result(name = "success", type = "redirect", location = "listCarPermission"),
			@Result(name = "error", type = "chain", location = "editCarPermission") })
	public String removeCarPermission(){
		if (ids==null) {
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
		this.getCarPermissionService().batchRemove(ids);
		return SUCCESS;
	}
}
